﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;
using log4net;

public partial class shift : System.Web.UI.Page
{
    //inject shiftBLL
    private IShiftBLL shiftBLL = Factory<IShiftBLL>.CreateInstance();
    //Inject log4net
    private static readonly ILog logger = LogManager.GetLogger(typeof(shift).Name);
    protected void Page_Load(object sender, EventArgs e)
    {       
            Inits();    
    }


    //Init page
    private void Inits()
    {
        try
        {
            rptShift.DataSource = shiftBLL.GetAll();
            rptShift.DataBind(); 
        }
        catch (Exception ex)
        {
            lblMsg.Text = Constants.ERROR_MSG;
            if (logger.IsErrorEnabled) { logger.Error(ex.Message); }
        }
         
    }

    private void ShiftActive(CheckBox chk, TextBox[] text, DateTime[] value)
    {
        chk.Checked = true;
        for (int i = 0; i < text.Length; i++)
        {
            text[i].Text = value[i].ToString(Constants.TIME_PATTERN);
            text[i].ReadOnly = false;
            text[i].CssClass = "width_102";
        }
    }

    private void ShiftUnactive(CheckBox chk, TextBox[] text)
    {
        chk.Checked = false;
        for (int i = 0; i < text.Length; i++)
        {
            text[i].Attributes.Add("readonly","readonly");
            text[i].CssClass = "width_102 disable";
            text[i].Text = "";
        }
    }

    ///// <summary>
    ///// Save data
    ///// </summary>
    private void Save()
    {
       

        try
        {
            //init BO and list
            Shift shift = null;
            List<Shift> list = new  List<Shift>();

            foreach (RepeaterItem item in rptShift.Items)
            {
                //find controls
                CheckBox chkShift = (CheckBox)item.FindControl("chkShift");
                TextBox txtFrom = (TextBox)item.FindControl("txtFrom");
                TextBox txtTo = (TextBox)item.FindControl("txtTo");
                HiddenField hdnId = (HiddenField)item.FindControl("hdnId");
               
                shift = new Shift();

                if (chkShift.Checked)
                {
                    //set to BO
                    shift.Id = Byte.Parse(hdnId.Value);
                    shift.FromTime = DateTime.ParseExact(Utils.StringCurrentDate() + Constants.SPACE + txtFrom.Text, Constants.DATE_TIME_PATTERN, new CultureInfo(Constants.CULTURE_VN));
                    shift.ToTime = DateTime.ParseExact(Utils.StringCurrentDate() + Constants.SPACE + txtTo.Text, Constants.DATE_TIME_PATTERN, new CultureInfo(Constants.CULTURE_VN));
                }
                else {
                    //set to BO
                    shift.Id = Byte.Parse(hdnId.Value);
                    shift.FromTime = DateTime.ParseExact(Utils.StringCurrentDate(), Constants.DATE_PATTERN, new CultureInfo(Constants.CULTURE_VN));
                    shift.ToTime = DateTime.ParseExact(Utils.StringCurrentDate(), Constants.DATE_PATTERN, new CultureInfo(Constants.CULTURE_VN));
                }

                shift.Active = chkShift.Checked;

                //add list
                list.Add(shift);
            }

            if (shiftBLL.Save(list))
            {
                Inits();
                lblMsg.Text = "Lưu thành công.";
            }
        }
        catch (Exception ex)
        {
            lblMsg.Text = Constants.ERROR_MSG;
            if (logger.IsErrorEnabled) { logger.Error(ex.Message); }
        }

    }



    protected void rptShift_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            //find controls
            TextBox txtFrom = (TextBox)e.Item.FindControl("txtFrom");
            TextBox txtTo = (TextBox)e.Item.FindControl("txtTo");
            CheckBox chkShift = (CheckBox)e.Item.FindControl("chkShift");
            HiddenField hdnActive = (HiddenField)e.Item.FindControl("hdnActive");

            //If shift is active
            if (!Boolean.Parse(hdnActive.Value))
            {
                ShiftUnactive(chkShift, new TextBox[] { txtFrom, txtTo });
            }

            //add javascript
            chkShift.InputAttributes["onchange"] = "Shift(this,"+(e.Item.ItemIndex + 1)+")";
        }
    }

    

    protected void btnSave_Click(object sender, ImageClickEventArgs e)
    {
            Save();
    }
}